package com.xunwulian.cmpp.sms;

import com.xunwulian.base.util.GsonUtil;
import com.xunwulian.base.util.TextUtil;
import org.codehaus.jackson.annotate.JsonIgnore;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

public class ShortMessage extends ByteData implements Serializable {

    @JsonIgnore
    private static final long serialVersionUID = -7358360500166470977L;

    byte msgFormat = 0;

    byte[] messageData = null;

    @JsonIgnore
    String encoding = "US-ASCII";

    public ShortMessage() {
    }

    public ShortMessage(String content) {

        /**
         * 包含汉字，则只能采用8，且短信会有后缀
         */
        if (TextUtil.isChinese(content)) {
            this.setMessage(new String(content.getBytes()), (byte) 8);
        } else {
            this.setMessage(new String(content.getBytes()), (byte) 0);
        }
    }

    @Override
    public ByteBuffer getData() {
        return new ByteBuffer(messageData);
    }

    public int getLength() {
        return messageData == null ? 0 : messageData.length;
    }

    public void setMessage(byte[] messageData, byte msgFormat) {
        this.messageData = messageData;
        this.msgFormat = msgFormat;
        setMsgFormat(msgFormat);
    }

    public void setMessage(String msg, byte msgFormat) {
        setMsgFormat(msgFormat);
        try {
            this.messageData = msg.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            logger.warn("unsupportted msgFormat!", e);
        }
    }

    public String getMessage() {
        String str = "";
        try {
            logger.info("encoding:" + encoding + ", msgFormat:" + msgFormat + ",message:" + new String(messageData,
                    encoding));
            str = new String(messageData, encoding);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NullPointerException e) {
            //
        }
        return str;
    }

    public String dump() {
        return GsonUtil.toJson(this, this.getClass());
    }

    public byte getMsgFormat() {
        return msgFormat;
    }

    @Override
    public void setData(ByteBuffer buffer) throws PDUException {
        this.messageData = buffer.getBuffer();
    }

    //    public void setData(byte[] data) {
    //        this.messageData = data;
    //    }

    public void setMsgFormat(byte msgFormat) {
        this.msgFormat = msgFormat;
        if (msgFormat == 0) {
            encoding = "US-ASCII";
        } else if (msgFormat == 4) {
            encoding = SmsConstant.ENC_ISO8859_1;
        } else if (msgFormat == 8) {
            encoding = "UnicodeBigUnmarked";
        } else if (msgFormat == 15) {
            encoding = "GBK";
        }
    }

    @JsonIgnore
    public String getEncoding() {
        return encoding;
    }

    @JsonIgnore
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
    //    public void setSm(String msg, byte msgFormat) {
    //        setMsgFormat(msgFormat);
    //        try {
    //            setData(msg.getBytes(encoding));
    //        } catch (UnsupportedEncodingException e) {
    //            logger.warn("msgFormat unsupportted!", e);
    //        }
    //    }
}
